#include "../include/Agent.h"
#include <iostream>
using namespace std;



Agent::~Agent()
{
	while (_indicator.size() !=0){
		delete _indicator.back();
		_indicator.pop_back();
	}
	delete _date;
}


Agent::Agent(int newId, Bank& newBank, int amount, double firstYitra): User(newId, newBank, firstYitra), _indicator(vector<BuySellIndicator*>()), _amountToBuy(amount), _date(new Poco::DateTime())
{

	vector<int> riseFall = vector<int>();

	for (int i=0; i<14; i++){
		riseFall.push_back(1);
	}
	riseFall.at(0) = 8;
	riseFall.at(2) = 8;
	riseFall.at(4) = 3;
	riseFall.at(6) = 8;
	riseFall.at(8) = 3;
	riseFall.at(10) = 5;
	riseFall.at(12) = 5;



	int j=0;
	for (int i=0; i<7; i++){
		_indicator.push_back(new BuySellIndicator(riseFall.at(j), riseFall.at(j+1)));
		j=j+2;
	}
	for (int i=0; i<14; i++){
		cout<<riseFall.at(i)<<endl;
	}
}

std::string Agent::toStr(Poco::DateTime time) {
        std::ostringstream ostr;
		std::string sTime;
		ostr << time.day() << "/" << time.month() << "/" << time.year();
		sTime = ostr.str();
		return sTime;
}

SellEvent* Agent::generateSellEvent(int type){
	std::ostringstream strStream ;
	strStream << ass2::eUserName(ass2::Id2eUser(_id)) <<" generates selling event: "<<toStr(*_date)<<", "
			<<ass2::energyToString(ass2::eEnergy(type))<<", "<<_res.at(type).getAmount()<<" and "<<_energyValue.at(type)*90/100<<".";
	EnergyMarketLogger::Instance().Log(strStream , Poco::Message::PRIO_WARNING);
	return (new SellEvent(new Poco::DateTime(*_date), _res.at(type).getAmount(), _id,  ass2::eEnergy(type), _energyValue.at(type)*90/100));
}

BuyEvent* Agent::generateBuyEvent(int type){
	std::ostringstream strStream ;
	strStream << ass2::eUserName(ass2::Id2eUser(_id)) <<" generates buying event: "<<toStr(*_date)<<", "
			<<ass2::energyToString(ass2::eEnergy(type))<<", "<<_amountToBuy<<" and "<<_energyValue.at(type)*105/100<<".";
	EnergyMarketLogger::Instance().Log(strStream , Poco::Message::PRIO_WARNING);
	return (new BuyEvent(new Poco::DateTime(*_date), _amountToBuy, _id,  ass2::eEnergy(type), _energyValue.at(type)*105/100));
}

std::vector<BuyEvent*> Agent::generateBuyEvents(){
	std::vector<BuyEvent*> buyEvents = std::vector<BuyEvent*>();

	for (int i=0; i<7; i++){
		if(_indicator.at(i)->getBuy() == true)
			buyEvents.push_back(generateBuyEvent(i));
	}
	return buyEvents;
}

std::vector<SellEvent*> Agent::generateSellEvents(){
		std::vector<SellEvent*> sellEvents = std::vector<SellEvent*>();

		for (int i=0; i<7; i++){
			if(_indicator.at(i)->getSell() == true & _res.at(i).getAmount() >0 )
				sellEvents.push_back(generateSellEvent(i));
		}
		return sellEvents;
	}

void Agent::countUp (ass2::eEnergy type){
	_indicator.at(type)->countUp();
}

void Agent::countDown (ass2::eEnergy type){
	_indicator.at(type)->countDown();
}

void Agent::setDate(Poco::DateTime * date){
	Poco::Timespan oneDay = Poco::Timespan(1,0,0,0,0);
	*_date += oneDay;
}
void Agent::setNewValues(std::vector<double> energyValue){
	_energyValue = energyValue;
}
